using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleRcon
{
    /// <summary>
    /// A collection of bookmarks
    /// </summary>
    public class BookmarkCollection
    {
        /// <summary>
        /// Add a item.
        /// </summary>
        /// <param name="item">The item.</param>
        public static void AddItem(Bookmark item)
        {
            if (Properties.Settings.Default.Bookmarks == null)
            {
                Properties.Settings.Default.Bookmarks = new System.Collections.Specialized.StringCollection();
            }

            Properties.Settings.Default.Bookmarks.Add(string.Format("{0};{1};{2}", item.Name, item.Address, item.Password));
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// Remove a item.
        /// </summary>
        /// <param name="item">The item.</param>
        public static void RemoveItem(Bookmark item)
        {
            Properties.Settings.Default.Bookmarks.Remove(string.Format("{0};{1};{2}", item.Name, item.Address, item.Password));
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// Gets the bookmarks.
        /// </summary>
        /// <value>The items.</value>
        public static Bookmark[] Items
        {
            get
            {
                List<Bookmark> result = new List<Bookmark>();

                if (Properties.Settings.Default.Bookmarks != null)
                {
                    foreach (var item in Properties.Settings.Default.Bookmarks)
                    {
                        result.Add(parseFromString(item));
                    }
                }

                return result.ToArray();
            }
        }

        private static Bookmark parseFromString(string str)
        {
            Bookmark item = new Bookmark();
            item.Name = str.Split(';')[0];
            item.Address = str.Split(';')[1];
            item.Password = str.Split(';')[2];
            return item;
        }
    }
}
